package com.ysd.lis.controller.lab;

import com.alibaba.excel.EasyExcel;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.synItemData.KnowBase;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.StaDictItemSyncDto;
import com.ysd.lis.request.lab.FindItemDictKnoInfoParam;
import com.ysd.lis.request.lab.LabItemDictDto;
import com.ysd.lis.request.qc.QcInstrSampleParam;
import com.ysd.lis.service.lab.LabItemDictService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yangjianguang
 * @since 2023-12-11
 */
@Api(tags = "报告项目")
@RestController
@RequestMapping("/lis/lab/item")
public class LabItemDictController extends BaseController {

    @Autowired
    private LabItemDictService labItemDictService;

    @ApiOperation("获取报告项目")
    @GetMapping("/findLabItemDictList")
    public Result findLabItemDictList(LabItemDict labLabItemDict) {

        return labItemDictService.findLabItemDictList(labLabItemDict);
    }

    @ApiOperation("获取报告项目-手工抄录指标设置")
    @GetMapping("/findLabItemDictManualList")
    public Result findLabItemDictManualList(LabItemDict labLabItemDict) {

        return labItemDictService.findLabItemDictManualList(labLabItemDict);
    }

    @ApiOperation("添加报告项目")
    @PutMapping("/addLabItemDict")
    public Result addLabItemDict(@RequestBody LabItemDict labLabItemDict) {
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            labLabItemDict.setOrgId(sysUser.getOrgId());
        }
        return labItemDictService.addLabItemDict(labLabItemDict);
    }

    @ApiOperation("编辑报告项目")
    @PostMapping("/editLabItemDict")
    public Result editLabItemDict(@RequestBody LabItemDict labLabItemDict) {
        return labItemDictService.editLabItemDict(labLabItemDict);
    }

    @ApiOperation("根据id报告项目")
    @PostMapping("/deleteLabItemDict")
    public Result deleteLabItemDict(@RequestBody LabItemDict labLabItemDict) {
        return labItemDictService.deleteLabItemDict(labLabItemDict);
    }

    @ApiOperation("获取报告项目")
    @GetMapping("/findLabItemDict")
    public Result findLabItemDict(LabItemDict labLabItemDict) {
        return labItemDictService.findLabItemDict(labLabItemDict);
    }

    @ApiOperation("获取报告项目")
    @GetMapping("/findJoinLabItemDictList")
    public Result findJoinLabItemDictList(LabItemDict labLabItemDict) {
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            labLabItemDict.setOrgId(sysUser.getOrgId());
        }
        return labItemDictService.findJoinLabItemDictList(labLabItemDict);
    }

    @ApiOperation("分页查询")
    @GetMapping("/findLabItemPageList")
    public Result findLabItemPageList(CommonPageDto pageParam) {
        return labItemDictService.findLabItemPageList(pageParam);
    }

    @ApiOperation("分页查询")
    @GetMapping("/findLabItemPageList1")
    public Result findLabItemPageList1(CommonPageDto pageParam) {
        return labItemDictService.findLabItemPageList1(pageParam);
    }

    @ApiOperation("获取报告项目")
    @GetMapping("/findLabItemDictListByInstr")
    public Result findLabItemDictListByInstr(QcInstrSampleParam param) {
        return labItemDictService.findLabItemDictListByInstr(param);
    }

    @ApiOperation("获取报告项目")
    @GetMapping("/findLabItemDictAndAllListByInstr")
    public Result findLabItemDictAndAllListByInstr(QcInstrSampleParam param) {
        return labItemDictService.findLabItemDictAndAllListByInstr(param);
    }

    @ApiOperation("同步报告项目")
    @GetMapping("/syncItemDicts")
    public Result syncItemDicts() {
        return labItemDictService.syncItemDicts();
    }

    @ApiOperation("批量修改顺序")
    @PostMapping("/updateMoveItemDictSeq")
    public Result updateMoveItemDictSeq(@RequestBody LabItemDictDto dto) {
        return labItemDictService.updateMoveItemDictSeq(dto);
    }

    @ApiOperation("知识库获取所有项目")
    @GetMapping("/findAllItemDictList")
    public Result findAllItemDictList(LabItemDict labItemDict) {
        return labItemDictService.findAllItemDictList(labItemDict);
    }

    @ApiOperation("获取项目知识库")
    @GetMapping("/findItemDictKnoInfo")
    public Result findItemDictKnoInfo(FindItemDictKnoInfoParam input) {
        return labItemDictService.findItemDictKnoInfo(input);
    }

    @ApiOperation("批量对照报告项目")
    @PostMapping("/batchSaveDictItem")
    public Result batchSaveDictItem(@RequestBody StaDictItemSyncDto itemSyncDto) {
        return labItemDictService.batchSaveDictItem(itemSyncDto);
    }


    @ApiOperation("导出字典")
    @GetMapping("/export")
    public void export(String rptGroup,  HttpServletResponse response) {
        labItemDictService.export(rptGroup, response);
    }

    @ApiOperation("上传字典")
    @PostMapping("/uploadDetailByExcel")
    public Result uploadDetailByExcel(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws Exception {

       return labItemDictService.uploadDetailByExcel(file, request);
    }


    @ApiOperation("导出知识库模板")
    @GetMapping("/exportBaseTemplate")
    public void exportBaseTemplate( HttpServletResponse response) throws IOException {

        // 设置响应信息，让浏览器下载文件
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("知识库模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // 向Excel中写入数据
        EasyExcel.write(response.getOutputStream(), KnowBase.class)
                .sheet("sheet")
                .doWrite(new ArrayList<>());
    }

    @ApiOperation("导入知识库模板")
    @PostMapping("/uploadBaseTemplate")
    public Result uploadBaseTemplate(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws Exception {

        return labItemDictService.uploadBaseTemplate(file, request);
    }

}
